﻿using CommandService;
using CommandService.Common.Base;
using Entity;
using Entity.SysEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
  public  class BaseBLL
    {
       Commands command;
       public BaseBLL()
        {
            command = new Commands();
        }
   
      /// <summary>
      /// 更新参数转成键值对格式
      /// </summary>
      /// <param name="arr">参数集合</param>
      /// <returns></returns>
       private List<Dictionary<string, object>> ParameterToDictionary(string[] arr)
       {
           var data = arr[0].ToString().Split('&');
           List<Dictionary<string, object>> dicList = new List<Dictionary<string, object>>();
           Dictionary<string, object> dic = new Dictionary<string, object>();
           foreach (string str in data)
           {
               var newArr = str.Split('=');
               dic.Add(newArr[0].Trim(), newArr[1].Trim());
           }
           dicList.Add(dic);
           return dicList;
       }
    

  
    
    

      /// <summary>
      /// 表格方法配置
      /// </summary>
      /// <param name="mc">数据模型</param>
      /// <returns></returns>
      public ReturnResult SaveMethodPath(MethodConfig mc)
      {
          ReturnResult rsl = new ReturnResult();
          MethodConfig query = new MethodConfig();
          query.tableName = mc.tableName;
          var list = command.GetDataByObject<MethodConfig>(query);
          if (list.Count > 0)
          {
              rsl.Message = "该表已存在方法";
              return rsl;
          }
          mc.createUser = "admin";
          mc.createTime = DateTime.Now.ToString();
          mc.state = 1;
          return command.Add(mc);
      }
      /// <summary>
      /// 根据页面编码获取表信息
      /// </summary>
      /// <param name="webCode"></param>
      /// <returns></returns>
      public DbPageDataInfo GetTableBywebInfo(string webCode)
      {
          if (!string.IsNullOrEmpty(webCode))
          {
              var infos =command.GetWebTableStructure(webCode);
              var web = (List<DbPageDataInfo>)infos[0];
        
              if (web.Count > 0)
              {
                  return web[0];
              }
          }
          return null;
      }
      /// <summary>
      /// 类方法反射方法
      /// </summary>
      /// <param name="list">参数集合</param>
      /// <returns></returns>
      //private List<EntityStructure> Reflex(string tableName,List<EntityStructure> list,string operateType)
      //{
      //    List<EntityStructure> datas = new List<EntityStructure>();
      //    object[] par = new object[] { list };
      //    string path = this.GetTableMethod(tableName);
      //    if (!string.IsNullOrEmpty(path))
      //    {
      //        Type tp = Type.GetType(path);   //命名空间.类名
      //        if (tp != null)
      //        {
      //            object obj = Activator.CreateInstance(tp);  //通过反射创建类
      //            string methodName = "";
      //            if (operateType.Trim().ToUpper().Equals("ADD"))
      //            {
      //                methodName = "AddBefore";
      //            }
      //            else
      //            {
      //                methodName = "EditBefore";
      //            }
      //            MethodInfo mi = tp.GetMethod(methodName); //方法的名称
      //            if (mi != null)
      //            {
      //                return (List<EntityStructure>)mi.Invoke(obj, par);
      //            }
      //        }
      //        else
      //        {
      //            return list;
      //        }
      //    }
         
      //    return list;
      //}
     
     /// <summary>
     /// 获取表格方法
     /// </summary>
     /// <param name="tableName"></param>
     /// <returns></returns>
      private string GetTableMethod(string tableName)
      {
          MethodConfig mc = new MethodConfig();
          mc.tableName = tableName.Trim();
          var list = command.GetDataByObject<MethodConfig>(mc);
          if (list != null && list.Count>0)
          {
              return list[0].methodPath.Trim();
          }
          return "";
      }
    
    
      /// <summary>
      /// 字段属性配置
      /// </summary>
      /// <param name="fields"></param>
      /// <returns></returns>
      public ReturnResult SaveFieldsConfig(DbFieldConfig fields)
      {
          ReturnResult rsl = new ReturnResult();
          try
          {
              StringBuilder sb = new StringBuilder();
              sb.Append("select * from sys_fieldConfig_info where ");
              sb.Append("tableName='"+ fields.tableName.Trim() +"' and ");
              sb.Append("cloumnCode='"+ fields.cloumnCode.Trim() +"'");
              var dt = command.GetDataTable(sb.ToString());
              if (dt.Rows.Count > 0)
              {
                  rsl.Message =string.Format("{0}字段已设置",fields.cloumnCode);
                  return rsl;
              }
              fields.status = 1;
              fields.createtime = DateTime.Now.ToString();
              fields.createUser = "admin";
              rsl = command.Add(fields);
          }
          catch (Exception ex)
          {
              rsl.Message = ex.Message;
          }
          return rsl;
      }
      /// <summary>
      /// 获取数据列表
      /// </summary>
      /// <param name="WebCode">页面编码</param>
      /// <returns></returns>
      //public Dictionary<string, object> GetDataList(string tableName)
      //{
      //    StringBuilder sb = new StringBuilder();
      //    string fields = "";//查询字段名称   
      //    List<ListTitle> titleList = new List<ListTitle>();//数据表列名称
      //    ArrayList rowList = new ArrayList();//数据集合
      //    Dictionary<string, object> dic = new Dictionary<string, object>();
      //    ListTitle title;
      //    #region
      //    var showInfo = command.GetTableShowInfo(null,tableName,"List");
      //    showInfo = showInfo.OrderBy(k => k.orderNum).ToList();//根据字段排序进行升序排列
      //    #endregion
      //    if (showInfo.Count > 1)
      //    {
      //        tableName = showInfo[0].tableName;
      //        foreach (ViewDataCloumns table in showInfo)
      //        {
      //            fields += table.cloumnCode + ",";
      //            if (table.isHidden < 1)
      //            {
      //                title = new ListTitle();
      //                title.field = table.cloumnCode.Trim();
      //                title.title = table.cloumnName.Trim();
      //                titleList.Add(title); //ArrayList集合中添加键值
      //            }

      //        }
      //        if (showInfo[0].operateCode.Trim().ToUpper().Equals("Y"))
      //        {
      //            title = new ListTitle();
      //            title.field = "right";
      //            title.toolbar = "#barDemo";
      //            title.width = 170;
      //            title.align = "center";
      //            titleList.Add(title); //ArrayList集合中添加键值
      //        }


      //        sb.Append("select " + fields.Substring(0, fields.Length - 1) + " from " + tableName);
      //        var dt = command.GetDataTable(sb.ToString());//获取该数据的信息
      //        foreach (DataRow dataRow in dt.Rows)
      //        {
      //            Dictionary<string, object> dictionary = new Dictionary<string, object>();  //实例化一个参数集合
      //            foreach (DataColumn dataColumn in dt.Columns)
      //            {
      //                dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
      //            }
      //            rowList.Add(dictionary); //ArrayList集合中添加键值

      //        }
      //    }

      //    dic.Add("cols", titleList);
      //    dic.Add("data", rowList);
      //    return dic;
      //}
  }
}
